package com.coder4j.service;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.coder4j.entity.SDisplayInterfaceDetailEntity;
import com.coder4j.mapper.SDisplayInterfaceDetailMapper;
import com.coder4j.vo.SDisplayInterfaceDetailQuery;
import com.coder4j.vo.SDisplayInterfaceDetailVO;
import com.coder4j.starter.web.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

/**
 * 显示界面兴趣区详情
 * @author keller
 * @date 2025-03-18 16:22:26
 */
@Service
@Slf4j
public class SDisplayInterfaceDetailService extends BaseServiceImpl<SDisplayInterfaceDetailMapper,SDisplayInterfaceDetailEntity,SDisplayInterfaceDetailVO> {

    @Resource
    private SDisplayInterfaceDetailMapper mapper;

    @Override
    public SDisplayInterfaceDetailVO entity2Vo(SDisplayInterfaceDetailEntity entity) {
        SDisplayInterfaceDetailVO vo = new SDisplayInterfaceDetailVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public SDisplayInterfaceDetailEntity vo2Entity(SDisplayInterfaceDetailVO vo) {
        SDisplayInterfaceDetailEntity entity = new SDisplayInterfaceDetailEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public void updata(String id, JSONObject jsonObject){
        //先删除
        mapper.delById(id);
        JSONArray regions = jsonObject.getJSONArray("regions");
        for (Object region : regions) {
            JSONObject regionJson = JSONUtil.parseObj(region);
            SDisplayInterfaceDetailVO sDisplayInterfaceDetailVO = new SDisplayInterfaceDetailVO();
            sDisplayInterfaceDetailVO.setDisplayInterfaceId(Integer.parseInt(id));
            sDisplayInterfaceDetailVO.setTaskFlowDetailId(regionJson.getInt("taskId"));
            sDisplayInterfaceDetailVO.setInterestArea(regionJson.toString());
            sDisplayInterfaceDetailVO.setTaskFlow(jsonObject.getInt("taskFlowId"));
            this.baseSaveVO(sDisplayInterfaceDetailVO);
        }
    }


    public JSONArray getOne(Integer id) {
        List<SDisplayInterfaceDetailVO> list = mapper.getById(id);
        JSONArray objects = new JSONArray();
        for (SDisplayInterfaceDetailVO sDisplayInterfaceDetailVO : list) {
            objects.add(JSONUtil.parseObj(sDisplayInterfaceDetailVO.getInterestArea()));
        }
        return objects;
    }

}
